var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var app = getApp();

const eveScrollHei = 40;

Page(common.extendPage(PageBase, {
    data: {
        dataLoaded: false,
        scrollTop: 0,
        scrollAni: true,
        notices: [],
        noticeVisible: false,
        curNotice: {}
    },

    onLoad(options) {
        this._options = this.options;
        common.initApp({
            extSuc: () => {
                this._fetch();
            },
            loginSuc: () => {
                this.$loginSuc();
            },
            sysInfoSuc: (sysInfo) => {
                this.setData({
                    winHei: app.getWinHei(),
                });
            },
        });

    },

    onShareAppMessage: function(res) {
        if (res.from === 'button') {
            // 来自页面内转发按钮
            console.log(res.target)
        }
        return {
            title: this.data.shopInfo.title,
            path: '/pages/shopDetail/index?id=' + this._options.id,
            success: function(res) {
                // 转发成功
            },
            fail: function(res) {
                // 转发失败
            }
        }
    },

    /**
     * 拉取店铺详情
     * @return {[type]} [description]
     */
    _fetch() {
        ajax.G({
            url: '/market/Home/shopInfo',
            data: {
                shop_id: this._options.id
            },
            success: (res) => {
                var {
                    shopInfo = {}
                } = res.result;


                if (shopInfo.image_urls) {
                    shopInfo.image_urls = shopInfo.image_urls.split('|');
                    shopInfo.image_urls.some((item,index) => {
                        if (!item) {
                            shopInfo.image_urls.splice(index,1)
                        }
                    })
                }



                this.setData({
                    shopInfo,
                    dataLoaded: true,
                });
            }
        });
        ajax.G({
            url: '/market/Home/shopNotice',
            data: {
                shop_id: this._options.id
            },
            success: (res) => {
                this.setData({
                    notices: res.result.notice
                });
                this._autoScroll();
            }
        });
    },

    _autoScroll() {
        setTimeout(() => {
            var len = this.data.notices.length;
            var scrollTop = this._scrollTop || 0;
            var num = Math.round(scrollTop / eveScrollHei) + 1;

            if (num >= len) {
                num = 0;
            }
            scrollTop = num * eveScrollHei;
            this.setData({
                scrollAni: num != 0,
            });
            this.setData({
                scrollTop
            });

            this._autoScroll();
        }, 2000);
    },

    _scrollNotice(e) {
        this._scrollTop = e.detail.scrollTop;
    },

    _scrollTouchend(e) {
        var scrollTop = this._scrollTop || 0;

        scrollTop = Math.round(scrollTop / eveScrollHei) * eveScrollHei;
        this.setData({
            scrollTop
        });
    },

    _showNotice(e) {
        var {
            item
        } = e.currentTarget.dataset;

        if (item) {
            this.setData({
                noticeVisible: true,
                curNotice: item,
            });
        }
    },

    _closeModal() {
        this.setData({
            noticeVisible: false,
        });
    }

}));